import { defineStore } from 'pinia';
import { baseState } from '@/store/modules/base/types';
import { OrgVo } from '@/api/organization';

const baseStore = defineStore('base', {
  state: (): baseState => ({
    orgInfo: {
      address: '',
      adminer: '',
      authControl: true,
      code: '',
      createOrgId: '',
      createOrgName: '',
      createTime: '',
      creator: '',
      creatorName: '',
      deleteFlag: '',
      email: '',
      hasAuthCreateOrgCollection: [],
      hasAuthCreatorCollection: [],
      id: '',
      name: '',
      newVersionTime: 0,
      operator: '',
      operatorName: '',
      organizationLeader: '',
      organizationTopLeader: '',
      parentId: '',
      phone: '',
      postCode: '',
      simpleName: '',
      sortNumber: 0,
      status: '',
      summary: '',
      tenantId: '',
      type: '',
      versionTime: 0,
    },
  }),
  getters: {
    org(state: baseState): OrgVo {
      return <OrgVo>state.orgInfo;
    },
  },

  actions: {
    updateSettings(partial: Partial<baseState>) {
      // @ts-ignore-next-line
      this.$patch(partial);
    },
    setOrgInfo(value: any) {
      // @ts-ignore
      this.orgInfo = value;
      if (value) {
        let arr = [];
        if (Array.isArray(value)) {
          arr = value;
        } else {
          arr = [value];
        }
        localStorage.setItem('selectedOrg', JSON.stringify(arr));
      } else {
        localStorage.removeItem('selectedOrg');
      }
    },
  },
});

export default baseStore;
